package cn.daniellee.plugin.pr;

import cn.daniellee.plugin.pr.command.RedstoneCommand;
import cn.daniellee.plugin.pr.command.RedstoneCompleter;
import cn.daniellee.plugin.pr.listener.BlockListener;
import cn.daniellee.plugin.pr.storage.Storage;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

public class PlotRedstone extends JavaPlugin {

    private static PlotRedstone instance;

    private String prefix;

    private Storage storage;

//    private BukkitTask missionTask;

    @Override
    public void onEnable() {
        instance = this;

        if (loadConfig()) {
            getLogger().info(" ");
            getLogger().info(">>>>>>>>>>>>>>>>>>>>>>>> PlotRedstone Loaded <<<<<<<<<<<<<<<<<<<<<<<<");
            getLogger().info(">>>>> If you encounter any bugs, please contact author's QQ: 768318841 <<<<<");
            getLogger().info(" ");

//            Bukkit.getPluginManager().registerEvents(new MenuListener(), this);
            Bukkit.getPluginManager().registerEvents(new BlockListener(), this);

            Bukkit.getPluginCommand("plotredstone").setExecutor(new RedstoneCommand());
            Bukkit.getPluginCommand("plotredstone").setTabCompleter(new RedstoneCompleter());

//            missionTask = new RedstoneRunnable().runTaskTimer(this, 0, 200); // 10秒执行一次
        }
    }

    public boolean loadConfig() {
        getLogger().info("[PlotRedstone] Loading config...");
        if(!getDataFolder().exists()) getDataFolder().mkdirs();
//        if (storage != null) storage.close();
//        storage = getConfig().getBoolean("storage.mysql.use", false) ? new MysqlStorage() : new SqliteStorage();
//        if (storage.initialize()) {
//            getLogger().info("[PlotRedstone]Storage initialized.");
//        } else {
//            getLogger().info(" ");
//            getLogger().info("[PlotRedstone]Initializing data store failed, please edit the config and reload the plugin.".replace("&", "§"));
//            getLogger().info(" ");
//            Bukkit.getPluginManager().disablePlugin(this);
//            return false;
//        }
        prefix = "&7[&6" + getConfig().getString("prompt-prefix", "PlotRedstone") + "&7] &3: &r";
        saveDefaultConfig();
        return true;
    }

    @Override
    public void onDisable() {
//        if (missionTask != null) missionTask.cancel();
//        storage.close();
        getLogger().info(" ");
        getLogger().info(">>>>>>>>>>>>>>>>>>>>>>>> PlotRedstone Unloaded <<<<<<<<<<<<<<<<<<<<<<<<");
        getLogger().info(" ");
    }

    public static PlotRedstone getInstance() {
        return instance;
    }

    public String getPrefix() {
        return prefix;
    }

	public Storage getStorage() {
		return storage;
	}
}
